# -*- coding: utf-8 -*-
"""
API 响应数据模型

@author: AI Assistant
"""
from typing import Optional, Dict, Any, List
from datetime import datetime
from pydantic import BaseModel, Field


class SessionDataResponse(BaseModel):
    """会话数据响应"""
    success: bool = Field(..., description="是否成功")
    message: str = Field(..., description="响应消息")
    data: Optional[Dict[str, Any]] = Field(None, description="会话数据")
    
    class Config:
        json_schema_extra = {
            "example": {
                "success": True,
                "message": "成功加载会话数据",
                "data": {
                    "session_id": "abc123",
                    "stcd": "21401550",
                    "stcd_name": "大伙房水库",
                    "model_type": "dhf",
                    "initial_state": {},
                    "parameters": {},
                    "para_schema": {}
                }
            }
        }


class ChatResponse(BaseModel):
    """对话响应"""
    success: bool = Field(..., description="是否成功")
    message: str = Field(..., description="LLM 回复消息")
    conversation_id: str = Field(..., description="对话ID")
    understood: bool = Field(..., description="是否理解用户意图")
    intent_type: Optional[str] = Field(None, description="用户意图类型：rainfall_scenario(降雨假拟) / parameter_modification(参数修改) / unknown(未知意图)")
    parameter_changes: Optional[Dict[str, Any]] = Field(None, description="参数修改建议")
    explanation: Optional[str] = Field(None, description="参数修改的详细说明")
    current_parameters: Optional[Dict[str, Any]] = Field(None, description="当前参数状态（修改后）")
    validation_errors: Optional[List[str]] = Field(None, description="参数验证错误（如果有）")
    update_success: Optional[bool] = Field(None, description="参数是否成功更新到水文模型")
    update_message: Optional[str] = Field(None, description="参数更新结果消息")
    update_data: Optional[Dict[str, Any]] = Field(None, description="更新接口返回的数据")
    parameter_ranges: Optional[Dict[str, List[Any]]] = Field(None, description="参数的取值范围")
    rainfall_simulation: Optional[Dict[str, Any]] = Field(None, description="降雨假拟信息（仅当意图为降雨假拟时返回）")
    
    class Config:
        json_schema_extra = {
            "example": {
                "success": True,
                "message": "好的，我将初始水位从 100.00 m 修改为 100.10 m",
                "conversation_id": "conv_20240101_001",
                "understood": True,
                "intent_type": "parameter_modification",
                "parameter_changes": {
                    "initial_state.water_level": 100.10
                },
                "explanation": "根据您的要求，将初始水位提高了 10cm（0.1m）",
                "current_parameters": {
                    "initial_state": {"water_level": 100.10},
                    "parameters": {}
                },
                "validation_errors": None,
                "update_success": True,
                "update_message": "参数更新成功",
                "update_data": {
                    "session_id": "xxx",
                    "updated": True
                },
                "parameter_ranges": {
                    "initial_state.water_level": [0, 200],
                    "parameters.B": [0, 1]
                },
                "rainfall_simulation": None
            }
        }


class ParameterValidationResponse(BaseModel):
    """参数验证响应"""
    success: bool = Field(..., description="是否验证通过")
    message: str = Field(..., description="验证消息")
    valid: bool = Field(..., description="参数是否合法")
    errors: Optional[List[Dict[str, Any]]] = Field(None, description="验证错误列表")
    warnings: Optional[List[Dict[str, Any]]] = Field(None, description="验证警告列表")
    
    class Config:
        json_schema_extra = {
            "example": {
                "success": True,
                "message": "参数验证通过",
                "valid": True,
                "errors": None,
                "warnings": [
                    {
                        "parameter": "parameters.K",
                        "message": "参数值接近上限",
                        "current_value": 0.95,
                        "valid_range": [0.0, 1.0]
                    }
                ]
            }
        }


class ConversationHistoryResponse(BaseModel):
    """对话历史响应"""
    success: bool = Field(..., description="是否成功")
    message: str = Field(..., description="响应消息")
    conversation_id: str = Field(..., description="对话ID")
    history: List[Dict[str, Any]] = Field(..., description="对话历史记录")
    total_count: int = Field(..., description="总记录数")
    
    class Config:
        json_schema_extra = {
            "example": {
                "success": True,
                "message": "成功获取对话历史",
                "conversation_id": "conv_20240101_001",
                "history": [
                    {
                        "timestamp": "2024-01-01T12:00:00",
                        "role": "user",
                        "message": "请将初始水位提高 10cm"
                    },
                    {
                        "timestamp": "2024-01-01T12:00:05",
                        "role": "assistant",
                        "message": "好的，我将初始水位从 100.00 m 修改为 100.10 m"
                    }
                ],
                "total_count": 2
            }
        }


class ErrorResponse(BaseModel):
    """错误响应"""
    success: bool = Field(False, description="是否成功")
    message: str = Field(..., description="错误消息")
    error_code: Optional[str] = Field(None, description="错误代码")
    details: Optional[Dict[str, Any]] = Field(None, description="错误详情")
    
    class Config:
        json_schema_extra = {
            "example": {
                "success": False,
                "message": "会话不存在",
                "error_code": "SESSION_NOT_FOUND",
                "details": {
                    "session_id": "abc123"
                }
            }
        }

